package com.googlecode.openauth.web.service;

import java.util.List;

import com.googlecode.openauth.web.dao.IAreaDao;
import com.googlecode.openauth.web.entity.AreaInfo;

public class AreaService implements IAreaService {
	private IAreaDao areaDao;

	public IAreaDao getAreaDao() {
		return areaDao;
	}

	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public String getParentAreaName(int areaId) {
		String areaStr = "";
		AreaInfo area = areaDao.getAreaLevelByid(areaId);
		areaStr = area.getAreaName();

		if (area.getParentId() != 0) {
			areaStr = getParentAreaName(area.getParentId()) + "-->" + areaStr;
		}
		return areaStr;
	}

	@Override
	public List<AreaInfo> getAreas(int areaId) {
		List<AreaInfo> list = areaDao.getAreaActionByAreaId(0);
		for (AreaInfo area : list) {
			String st = this.getParentAreaName(area.getAreaId());
			int idx = st.lastIndexOf("-->");
			if (idx != -1) {
				area.setParentAreas(st.substring(0, idx));
			} else {
				area.setParentAreas(st);
			}
		}
		return list;
	}

}
